﻿using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;

namespace Gavel.ModLink.ViewModel;

/// <summary>
/// 报文视图模型
/// </summary>
public class MessageViewModel : INotifyPropertyChanged
{
    /// <summary>
    /// 属性修改事件
    /// </summary>
    public event PropertyChangedEventHandler? PropertyChanged;
    
    /// <summary>
    /// 报文列表
    /// </summary>
    public ObservableCollection<string> Messages { get; set; } = [];

    /// <summary>
    /// 报文视图内容
    /// </summary>
    public string Content
    {
        get => string.Join(Environment.NewLine, Messages);
        set => _ = value;
    }

    public MessageViewModel()
    {
        Messages.CollectionChanged += CollectionChanged;
    }

    /// <summary>
    /// 报文列表改变事件
    /// </summary>
    /// <param name="sender">事件发送者</param>
    /// <param name="e">事件参数</param>
    private void CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
    {
        if (Messages.Count > 200)
        {
            Messages.RemoveAt(0);
        }
        
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Content)));
    }
}